package com.morningstar.flow.flowable.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.morningstar.flow.antvX6.common.Assign;
import com.morningstar.flow.service.FlowInstanceTimeLineService;
import com.morningstar.flow.service.FlowNodeService;
import com.morningstar.flow.service.FlowProcessService;
import com.morningstar.flow.service.FlowTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.TaskService;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;

import static com.morningstar.flow.flowable.assigne.AssigneeStrategy.*;

/**
 * 处理审批人为空 策略
 *
 * @author Admin
 */
@Slf4j
public class AssigneeListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        String assignee = delegateTask.getAssignee();
        String name = delegateTask.getName();
        log.info("AssigneeListener {} {}", name, assignee);
        // 审批人不为空
        if (StringUtils.isNotBlank(assignee) && !StringUtils.equalsIgnoreCase(assignee, NO_ASSIGN)) {
            // 审批人和发起人相同
            return;
        }
        // 审批人为空
        log.warn("AssigneeListener {} {} 审批人为空", name, assignee);
        TaskEntityImpl taskEntity = (TaskEntityImpl) delegateTask;
        FlowNodeService flowNodeService = SpringUtil.getBean(FlowNodeService.class);
        Assign assign = flowNodeService.getAssignByDefinitionId(taskEntity.getProcessDefinitionId(), taskEntity.getTaskDefinitionKey());
        // 通过
        if (StringUtils.equalsIgnoreCase(assign.getAssignEmpty(), ASSIGN_EMPTY_PASS)) {
            FlowTaskService flowTaskService = SpringUtil.getBean(FlowTaskService.class);
            flowTaskService.complete(taskEntity.getId(), true);
            return;
        }
        // 拒绝
        if (StringUtils.equalsIgnoreCase(assign.getAssignEmpty(), ASSIGN_EMPTY_REFUSE)) {
            FlowTaskService flowTaskService = SpringUtil.getBean(FlowTaskService.class);
            flowTaskService.complete(taskEntity.getId(), false);
            return;
        }
        // 转办给流程管理员
        if (StringUtils.equalsIgnoreCase(assign.getAssignEmpty(), ASSIGN_EMPTY_TO_ADMIN)) {
            TaskService taskService = SpringUtil.getBean(TaskService.class);
            FlowProcessService flowProcessService = SpringUtil.getBean(FlowProcessService.class);
            FlowInstanceTimeLineService flowInstanceTimeLineService = SpringUtil.getBean(FlowInstanceTimeLineService.class);
            String processAdmin = flowProcessService.getProcessAdmin(taskEntity.getProcessDefinitionId()).toString();
            taskService.setAssignee(taskEntity.getId(), processAdmin);
            flowInstanceTimeLineService.taskDeliver(taskEntity, Long.parseLong(processAdmin), "审批任务为空，转办给流程管理员");
            return;
        }
        // 转办给指定人员
        if (StringUtils.equalsIgnoreCase(assign.getAssignEmpty(), ASSIGN_EMPTY_TO_USER)) {
            if (StringUtils.isBlank(assign.getAssignEmptyUser())) {
                throw new RuntimeException("AssigneeListener 审批人为空时策略为转办给指定人员，转办人员为空");
            }
            TaskService taskService = SpringUtil.getBean(TaskService.class);
            taskService.setAssignee(taskEntity.getId(), assign.getAssignEmptyUser());
            FlowInstanceTimeLineService flowInstanceTimeLineService = SpringUtil.getBean(FlowInstanceTimeLineService.class);
            flowInstanceTimeLineService.taskDeliver(taskEntity, Long.parseLong(assign.getAssignEmptyUser()), "审批任务为空，转办给指定人员");
            return;
        }
        throw new RuntimeException("AssigneeListener 审批人为空时策略为空");
    }
}
